package com.example.h5shell;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.h5shell.utils.ContactManagerHelper;
import com.example.h5shell.utils.PermissionHelper;
import com.example.h5shell.utils.ToastHelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class SmsActivity extends AppCompatActivity implements View.OnClickListener , PermissionHelper.PermissionCallback{


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_sms);

        Button myButton = findViewById(R.id.button);

        myButton.setOnClickListener(this);

        Button myButton2 = findViewById(R.id.button2);
        myButton2.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        System.out.println(view);
        String tag = (String) view.getTag();
        if ("button".equals(tag)){
            // 动态申请权限
            boolean check = PermissionHelper.hasPermission(this, android.Manifest.permission.READ_CONTACTS);

            if (!check) {
                ToastHelper.show(this, "无权限，需要申请权限");
                String[] requestPerm = new String[]{Manifest.permission.READ_CONTACTS};
                PermissionHelper.requestPermissions(this, requestPerm, 101, this);
            }else {
                ToastHelper.show(this, "有权限");
                onPermissionGranted();
            }

        }

        if ("button2".equals(tag)){
            // 动态申请权限
            boolean check = PermissionHelper.hasPermission(this, Manifest.permission.WRITE_CONTACTS);

            if (!check) {
                ToastHelper.show(this, "无权限，需要申请权限");
                String[] requestPerm = new String[]{Manifest.permission.WRITE_CONTACTS};
                PermissionHelper.requestPermissions(this, requestPerm, 102, this);
            }else {
                ToastHelper.show(this, "有权限");
                onPermissionGranted();
            }
        }

    }


    @Override
    public void onPermissionGranted() {
        // 读取联系人
        ContactManagerHelper contactManagerHelper = new ContactManagerHelper(this);
        ArrayList<HashMap<String, String>>  contacts = contactManagerHelper.readContacts();

        String message = String.format("读取到联系人： %s个", contacts.toArray().length);
        ToastHelper.show(this, message);
    }


    public void WriteContacts() {
        // 写入联系人
        ContactManagerHelper contactManagerHelper = new ContactManagerHelper(this);
        boolean result = contactManagerHelper.addContact("孟颖", "17783098377");
        ToastHelper.show(this, Boolean.toString(result));
    }



    @Override
    public void onPermissionDenied(List<String> deniedPermissions) {
        // 权限请求被用户拒绝
        System.out.println(deniedPermissions);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        // 你可以根据requestCode来判断是哪个请求的结果，并调用相应的逻辑
        if (requestCode == 101) {
            // 检查是否所有请求的权限都被授予
            boolean allGranted = true;
            for (int grantResult : grantResults) {
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    allGranted = false;
                    break;
                }
            }
            if (allGranted) {
                // 所有权限都被授予
                onPermissionGranted(); // 调用你的回调方法
            } else {
                // 至少有一个权限被拒绝
                onPermissionDenied(Arrays.asList(permissions)); // 传递被拒绝的权限列表到你的回调方法
            }
        }

        if (requestCode == 102) {
            // 检查是否所有请求的权限都被授予
            boolean allGranted = true;
            for (int grantResult : grantResults) {
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    allGranted = false;
                    break;
                }
            }
            if (allGranted) {
                // 所有权限都被授予
                WriteContacts(); // 调用你的回调方法
            } else {
                // 至少有一个权限被拒绝
                onPermissionDenied(Arrays.asList(permissions)); // 传递被拒绝的权限列表到你的回调方法
            }
        }
    }
}